package _1_leetcode_1_20._03无重复字符串的最长子串;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * 给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
 * https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
 *
 * @author ：zxl
 * @date ：Created in 2020/12/28 15:21
 */
public class ExcuteMain {

    public static void main(String[] args) {
        String input = "tmmzuxt";

        System.out.println(lengthOfLongestSubstring(input));
    }

    public static int lengthOfLongestSubstring(String s) {
        Set<Character> sub = new HashSet<Character>();
        int max = 0;
        Queue<Character> words = new LinkedList<Character>();
        for (int i = 0; i < s.length(); i++) {
            Character tmp = s.charAt(i);
            if (!sub.contains(tmp)) {
                sub.add(tmp);
                words.offer(tmp);
                if (words.size() > max) {
                    max = words.size();
                }
            } else {
                while(!words.isEmpty()){
                    Character tmp2 = words.poll();
                    sub.remove(tmp2);
                    if(tmp.equals(tmp2)){
                        break;
                    }
                }
                words.offer(tmp);
                sub.add(tmp);
            }

        }
        return max;
    }

}
